package com.sky.controller.user;

import com.sky.controller.BaseController;
import com.sky.dto.OrderPaymentDTO;
import com.sky.dto.OrderSubmitDTO;
import com.sky.result.Result;
import com.sky.result.TableDataInfo;
import com.sky.service.OrderService;
import com.sky.vo.OrderDetailVO;
import com.sky.vo.OrderSubmitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Api(tags = "用户订单相关接口")
@Slf4j
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result submit(@RequestBody OrderSubmitDTO dto) {
        OrderSubmitVO vo = orderService.submit(dto);
        return Result.success(vo);
    }

    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result orderDetail(@PathVariable Long id) {
        log.info("查询订单详情ID =>{}", id);
        OrderDetailVO detail = orderService.getDetail(id);
        return Result.success(detail);
    }

    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public TableDataInfo historyOrders(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pageSize,
                                       @RequestParam(required = false) Integer status) {
        log.info("查询历史订单 =>{},{},{}", page, pageSize, status);
        List<OrderDetailVO> historyOrders = orderService.getHistoryOrders(page, pageSize, status);
        return getTableInfo(historyOrders);
    }

    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancel(@PathVariable Long id) {
        log.info("取消订单 =>{}", id);
        orderService.cancel(id);
        return Result.success();
    }

    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result payment(@RequestBody OrderPaymentDTO paymentDTO) {
        log.info("订单支付 =>{}", paymentDTO);
        orderService.payment(paymentDTO);
        return Result.success();
    }

    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id) {
        log.info("再来一单 =>{}", id);
        orderService.repetition(id);
        return Result.success();
    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("客户催单")
    public Result reminder(@PathVariable Long id) {
        log.info("客户催单 =>{}", id);
        orderService.reminder(id);
        return Result.success();
    }
}
